package cn.sfrog.project.biz.admin.service.impl;

import cn.hutool.json.JSONUtil;
import cn.sfrog.common.exception.ApiException;
import cn.sfrog.common.utils.DateUtils;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizServiceOrder;
import cn.sfrog.project.biz.admin.domain.BizServiceOrderComplaint;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderComplaintDealWithVO;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderComplaintFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderComplaintQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderComplaintVO;
import cn.sfrog.project.biz.admin.mapper.BizServiceOrderComplaintMapper;
import cn.sfrog.project.biz.admin.service.IBizServiceOrderComplaintService;
import cn.sfrog.project.biz.web.vo.order.AppOrderComplaintVO;
import cn.sfrog.project.biz.web.vo.order.AppServiceOrderComplaintFormVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 订单投诉Service业务层处理
 */
@Service
public class BizServiceOrderComplaintServiceImpl extends ServiceImpl<BizServiceOrderComplaintMapper, BizServiceOrderComplaint> implements IBizServiceOrderComplaintService {
    @Resource
    private BizServiceOrderComplaintMapper bizServiceOrderComplaintMapper;

    /**
     * 查询订单投诉
     *
     * @param id 订单投诉主键
     * @return 订单投诉
     */
    @Override
    public BizServiceOrderComplaintVO selectBizServiceOrderComplaintById(Long id) {
        return bizServiceOrderComplaintMapper.selectBizServiceOrderComplaintById(id);
    }

    /**
     * 查询订单投诉列表
     *
     * @param query 订单投诉
     * @return 订单投诉
     */
    @Override
    public List<BizServiceOrderComplaintVO> selectBizServiceOrderComplaintList(BizServiceOrderComplaintQueryVO query) {
        if(query.getDateValue() != null){
            query.setStartTime(query.getDateValue()[0] + " 00:00:00");
            query.setEndTime(query.getDateValue()[1] + " 23:59:59");
        }
        return bizServiceOrderComplaintMapper.selectBizServiceOrderComplaintList(query);
    }

    /**
     * 新增订单投诉
     *
     * @param form 订单投诉
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizServiceOrderComplaint(BizServiceOrderComplaintFormVO form) {
        BizServiceOrderComplaint bizServiceOrderComplaint = new BizServiceOrderComplaint();
        BeanUtils.copyProperties(form, bizServiceOrderComplaint);
        bizServiceOrderComplaint.setCreateTime(new Date());
        bizServiceOrderComplaint.setCreateBy(SecurityUtils.getUsername());
        bizServiceOrderComplaint.setUpdateTime(new Date());
        bizServiceOrderComplaint.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceOrderComplaint.insert();
    }

    /**
     * 修改订单投诉
     *
     * @param form 订单投诉
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizServiceOrderComplaint(BizServiceOrderComplaintFormVO form) {
        BizServiceOrderComplaint bizServiceOrderComplaint = new BizServiceOrderComplaint();
        BeanUtils.copyProperties(form, bizServiceOrderComplaint);
        bizServiceOrderComplaint.setUpdateTime(new Date());
        bizServiceOrderComplaint.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceOrderComplaint.updateById();
    }

    /**
     * 批量删除订单投诉
     *
     * @param ids 需要删除的订单投诉主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizServiceOrderComplaintByIds(Long[] ids) {
            return BizServiceOrderComplaint.DAO.delete(Wrappers.lambdaQuery(BizServiceOrderComplaint.class).in(BizServiceOrderComplaint::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return true;
    }

    @Override
    public Boolean dealWith(BizServiceOrderComplaintDealWithVO dealWithVO) {
        LambdaUpdateWrapper<BizServiceOrderComplaint> updateWrapper =  new LambdaUpdateWrapper<BizServiceOrderComplaint>()
                .eq(BizServiceOrderComplaint::getId,dealWithVO.getId())
                .set(BizServiceOrderComplaint::getDealWithStatus,dealWithVO.getDealWithStatus())
                .set(BizServiceOrderComplaint::getRemark,dealWithVO.getRemark())
                .set(BizServiceOrderComplaint::getUpdateBy,SecurityUtils.getUsername())
                .set(BizServiceOrderComplaint::getUpdateTime, DateUtils.getNowDate());
        return BizServiceOrderComplaint.DAO.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean saveOrderComplaint(AppServiceOrderComplaintFormVO form) {
        Boolean update = true;
        BizServiceOrder bizServiceOrder = BizServiceOrder.DAO.selectById(form.getServiceOrderId());
        if(StringUtils.isNotNull(bizServiceOrder)){
            if(BizServiceOrder.COMPLAINT_STATUS_YES.equals(bizServiceOrder.getComplaintStatus())){
                throw new ApiException("您已经投诉过该订单了");
            }
            bizServiceOrder.setComplaintStatus(BizServiceOrder.COMPLAINT_STATUS_YES);
            bizServiceOrder.setUpdateTime(DateUtils.getNowDate());
            bizServiceOrder.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserName());
            update = bizServiceOrder.updateById();
        }
        if(update){
            BizServiceOrderComplaint complaint = new BizServiceOrderComplaint();
            complaint.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
            complaint.setContent(form.getContent());
            if(StringUtils.isNotEmpty(form.getImages())){
                String[] imageArr = form.getImages().split(",");
                complaint.setImages(JSONUtil.toJsonStr(imageArr));
            }
            complaint.setComplaintTime(DateUtils.getNowDate());
            if(StringUtils.isNotNull(bizServiceOrder)){
                complaint.setServiceOrderSn(bizServiceOrder.getServiceOrderSn());
            }
            complaint.insert();
        }

        return update;
    }

    @Override
    public List<AppOrderComplaintVO> selectOrderComplaintByUserId(Long userId) {
        List<AppOrderComplaintVO> voList = bizServiceOrderComplaintMapper.selectOrderComplaintByUserId(userId);
        for (AppOrderComplaintVO vo:voList) {
                if(StringUtils.isNotBlank(vo.getImages())){
                    List<String> imageList = JSONObject.parseArray(vo.getImages(), String.class);
                    vo.setImageList(imageList);
                }
        }
        return voList;
    }
}
